import { Component, OnInit } from '@angular/core';
import { CommonModule } from '@angular/common';
import { ActivatedRoute } from '@angular/router';
import { InventoryTransferService } from './inventory-transfer.service';
import { InventoryTransferDto } from './models';

@Component({
  selector: 'app-inventory-transfer-detail',
  templateUrl: './inventory-transfer-detail.component.html',
  styleUrls: ['./inventory-transfer-detail.component.scss'],
  standalone: true,
  imports: [CommonModule]
})
export class InventoryTransferDetailComponent implements OnInit {
  transfer: InventoryTransferDto | null = null;
  isLoading = true;
  currentTab: string = 'detail'; // 默认显示调拨明细

  constructor(
    private route: ActivatedRoute,
    private inventoryTransferService: InventoryTransferService
  ) {}

  ngOnInit(): void {
    const id = this.route.snapshot.paramMap.get('id');
    console.log('详情页id:', id);
    if (id) {
      this.inventoryTransferService.get(id).subscribe({
        next: (data) => {
          console.log('详情数据:', data);
          this.transfer = data;
          this.isLoading = false;
        },
        error: (err) => {
          console.error('详情接口报错', err);
          this.isLoading = false;
        }
      });
    }
  }

  getTotalQuantity(): number {
    const list = this.transfer?.merchandiseList || this.transfer?.details || [];
    return list.reduce((sum, d) => sum + Number(d.merchandiseNumber || d.MerchandiseNumber || 0), 0);
  }

  getTotalAmount(): number {
    const list = this.transfer?.merchandiseList || this.transfer?.details || [];
    return list.reduce((sum, d) => sum + Number(d.merchandisePrice || d.MerchandisePrice || 0), 0);
  }
} 